package com.item.yxlottery.yxlottery.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TableLayout;

import com.zhy.autolayout.AutoLayoutInfo;
import com.zhy.autolayout.utils.AutoLayoutHelper;

/*
 *------------------------------
 * @创建者      Administrator
 * @创建时间    2016/12/23 09:59
 * @文件描述    自适配的自定义TableLayout
 *------------------------------
 */

public class AutoTableLayout extends TableLayout {

    private AutoLayoutHelper mHelper = new AutoLayoutHelper(this);
    private Context mContext;
    private BaseAdapter mAdapter;// 数据适配
    private int column = 0;// 列数

    public AutoTableLayout(Context context) {
        super(context);
    }

    public AutoTableLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setStretchAllColumns(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!isInEditMode())
            mHelper.adjustChildren();
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }


    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }

    public static class LayoutParams extends TableLayout.LayoutParams
            implements AutoLayoutHelper.AutoLayoutParams {
        private AutoLayoutInfo mAutoLayoutInfo;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            mAutoLayoutInfo = AutoLayoutHelper.getAutoLayoutInfo(c, attrs);
        }

        @Override
        public AutoLayoutInfo getAutoLayoutInfo() {
            return mAutoLayoutInfo;
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }

        public LayoutParams(MarginLayoutParams source) {
            super(source);
        }
    }

    private void drawLayout() {
        removeAllViews();
        int realcount = mAdapter.getCount();
        int count = 0;
        if (realcount < column) {
            count = column;
        } else if (realcount % column != 0) {
            count = realcount + column - (realcount % column);
        } else {
            count = realcount;
        }
        AutoTableRow tableRow = null;//每一行的TableRow
        for (int i = 0; i < count; i++) {
            final int index = i;
            View view = null;
            if (index >= realcount) {
                view = mAdapter.getView((realcount - 1), null, null);
                view.setVisibility(View.INVISIBLE);
            } else {
                view = mAdapter.getView(index, null, null);
            }
            if (index % column == 0) {// 整行
                tableRow = new AutoTableRow(mContext);
            }
            if (tableRow != null) {//添加每一个Item
                tableRow.addView(view);
            }
            if (index % column == 0) {// 整行
                addView(tableRow, new TableLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }
        }
    }

    public void setAdapter(BaseAdapter baseAdapter, int column) {
        if (baseAdapter == null || baseAdapter.getCount() == 0) {
            return;
        }
        this.mAdapter = baseAdapter;
        this.column = column;
        drawLayout();
    }

    public void setAdapter(BaseAdapter baseAdapter) {
        setAdapter(baseAdapter, 4);
    }
}
